'use strict';
const db = uniCloud.database();
const roomsCollection = db.collection('rooms');

exports.main = async (event, context) => {
	const { room_id, token } = event;
	
	if (!room_id) {
		return {
			code: 400,
			message: '缺少房间ID'
		};
	}
	
	if (!token) {
		return {
			code: 401,
			message: '未登录'
		};
	}
	
	try {
		// 1. 获取房间信息，以获取图片列表
		const roomInfo = await roomsCollection.doc(room_id).get();
		const images = roomInfo.data[0]?.images || [];
		
		// 2. 删除云存储中的图片
		if (images.length > 0) {
			try {
				await uniCloud.deleteFile({
					fileList: images
				});
			} catch (e) {
				console.error('删除云存储图片失败', e);
			}
		}
		
		// 3. 删除房间记录
		await roomsCollection.doc(room_id).remove();
		
		return {
			code: 0,
			message: '删除成功'
		};
	} catch (error) {
		console.error('删除房间失败', error);
		return {
			code: 500,
			message: error.message || '删除失败'
		};
	}
}; 